package com.heima.behavior.service.impl;
import java.util.Date;

import com.heima.behavior.dtos.FollowBehaviorDto;
import com.heima.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 关注用户
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        // 1.校验参数
        if (dto == null || dto.getAuthorId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = ApThreadLocalUtil.getUser();
        if (apUser == null || apUser.getId().equals(0)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 2.根据用户id和作者id查询redis
        String followKey = "USER_FOLLOW" + apUser.getId();
        String fansKey = "USER_FANS" + dto.getAuthorId();
        Boolean followFlag = redisTemplate.opsForHash().hasKey(followKey, dto.getAuthorId().toString());
        // 3.如果已关注，并且继续要关注该作者，则终止
        if (followFlag && dto.getOperation() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "已关注该作者，无法继续关注");
        }
        // 4.如果已关注，并且要取消关注该作者，则删除redis和mongo
        if (followFlag && dto.getOperation() == 1) {
            redisTemplate.opsForHash().delete(followKey, dto.getAuthorId().toString());
            redisTemplate.opsForHash().delete(fansKey, apUser.getId().toString());
            mongoTemplate.remove(Query.query(
                    Criteria.where("userId").is(apUser.getId())
                            .and("followId").is(dto.getAuthorId())
            ), ApFollowBehavior.class);
        }
        // 5.如果没关注，并且要取消关注，则终止
        if (!followFlag && dto.getOperation() == 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "没关注该作者，无法取消关注");
        }
        // 6.如果没关注，并且要关注该作者，则新增redis和mongo
        if (!followFlag && dto.getOperation() == 0) {
            redisTemplate.opsForHash().put(followKey, dto.getAuthorId().toString(), "1");
            redisTemplate.opsForHash().put(fansKey, apUser.getId().toString(), "1");
            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setUserId(Long.valueOf(apUser.getId()));
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apFollowBehavior);
        }
        return ResponseResult.okResult(200, "关注用户成功");
    }
}
